﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Security.Cryptography;

namespace SDT.Libraries.DuplicateDetection
{
   public class Helper
    {
        HttpWebResponse res;
        Stream resStream;
        public bool isValidUrl(String url)
        {
            HttpWebRequest req;

            try
            {
                req = (HttpWebRequest)HttpWebRequest.Create(url);
                res = (HttpWebResponse)req.GetResponse();
                return (res.StatusCode == HttpStatusCode.OK);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error in ISValidURL " + ex.Message + " - " + url);
                return false;
            }



        }
        public void urlOpen()
        {
            resStream = res.GetResponseStream();
        }
        public String read()
        {
            String strText = "";
            try
            {
                StreamReader sr = new StreamReader(resStream);
                strText = sr.ReadToEnd();
            }
            catch
            { }

            return strText;
        }

        public string[] RemoveEmptyString(string[] words)
        {
            List<string> newList = new List<string>();
            foreach (string w in words)
            {
                if (!string.IsNullOrEmpty(w) & w != "" & w != " " & w != null & w != String.Empty)
                {
                    newList.Add(w);
                }

            }
            return newList.ToArray();

        }
        public string GetTextOnly(string page)
        {

            page = Regex.Replace(page, "<script.*?</script>", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);
            page = Regex.Replace(page, "<!--.*?-->", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);
            page = Regex.Replace(page, "<a.*?</a>", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);
            page = Regex.Replace(page, "<style.*?</style>", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);
            page = Regex.Replace(page, "<div.*?>", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);
            page = Regex.Replace(page, "</div>", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);
            page = Regex.Replace(page, "&nbsp;", string.Empty, RegexOptions.Multiline | RegexOptions.IgnoreCase);
            Regex regex = new Regex("<[^<>]*>", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            string temp = regex.Replace(page, String.Empty).Trim();
            temp = Regex.Replace(temp, @"[\t\n\r]", string.Empty, RegexOptions.Multiline | RegexOptions.IgnoreCase);

            return temp;

        }

        public List<string> gettagList(String HTMLcontent, String tag)
        {
            MatchCollection tagCollection;
            List<string> tagList = new List<string>();
            Regex extractTags = new Regex(@"<" + tag + @"\w*.*?>\s*\t*\n*\r*.*?\s*\t*\n*\r*</" + tag + ">", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            try
            {
                tagCollection = extractTags.Matches(HTMLcontent);


                foreach (Match m in tagCollection)
                    // MessageBox.Show(Regex.Replace(m.Value, "\\</*" + tag + ".*?>", String.Empty));//ToString());
                    //tagList.Add(Regex.Replace(m.Value,"\\</*" + tag + ".*?>", String.Empty));

                    tagList.Add(Regex.Replace(m.Value, @"<(.|\n)*?>", string.Empty));
                return tagList;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return null;
        }

        public string CalculateMD5Hash(string input)
        {
            // step 1, calculate MD5 hash from input
            MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
            byte[] hash = md5.ComputeHash(inputBytes);

            // step 2, convert byte array to hex string
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }
            return sb.ToString();
        }
    }
}
